<?php
/**
 * File Name...: helpers\string.php
 * Framework...: Blackbird Framework
 * Author......: KnKill, 2010 (c) KnKill Corporation
 */



/**
 * @package Blackbird Framework
 * @subpackage Helpers
 * @since 0.2
 */
class StringHelpers
{


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $string
 * @return string
 */
static public function trim_slashes($string) {
    // Ужасно полезная функция, хуле
    return trim($string, '/');
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param mixed $source
 * @return mixed
 */
static public function strip_slashes($source) {
    if (is_array($source)) {
        foreach ($source as $key => $value) {
            $source[$key] = strip_slashes($value);
        }
    } else {
        $source = stripslashes($source);
    }
    return $source;
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $string
 * @return string
 */
static public function strip_quotes($string) {
    $quotes = array('"', "'");
    return str_replace($quotes, '', $string);
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $string
 * @return string
 */
static public function quotes_to_entities($string) {
    $quotes   = array("\'","\"","'",'"'); // Кавычки
    $entities = array("&#39;","&quot;","&#39;","&quot;"); // Их HTML сущности
    // Заменяем кавычки и косые линии на их HTML-сущности
    return str_replace($quotes, $entities, $string);
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $string
 * @return string
 */
static public function reduce_double_slashes($string) {
    return preg_replace("#([^:])//+#", "\\1/", $string);
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $string
 * @param string $character
 * @param bool $trim
 */
static public function reduce_multiples($string, $character = ',', $trim = false) {
    $str = preg_replace('#'. preg_quote($character, '#') . '{2,}#', $character, $string);
    if ($trim) {
        $string = trim($string, $character);
    }
    return $string;
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $type
 * @param $lenght
 * @return mixed
 */
static public function random_string($type = 'alnum', $lenght = 8) {
    switch ($type) {
        case 'alnum':
        case 'numeric':
        case 'nozero':
            switch ($type) {
                case 'alnum':
                    $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                    break;
                case 'numeric':
                    $pool = '0123456789';
                    break;
                case 'nozero':
                    $pool = '123456789';
                    break;
            }
            $string = '';
            for ($i = 0; $i < $lenght; $i++) {
                $string .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
            }
            return $string;
            break;
        case 'unique':
            return md5(uniqid(mt_rand()));
            break;
    }
}


/**
 * @static
 * @access private
 * @since 0.2
 * @param string Параметров может быть несколько
 * @return mixed
 */
static public function alternator() {
    static $i;
    if (func_num_args() == 0) {
        $i = 0;
        return '';
    }
    $args = func_get_args();
    return $args[($i++ % count($args))];
}


/**
 * @static
 * @access public
 * @since 0.2
 * @param string $data
 * @param integer $count
 * @return string
 */
static public function repeater($data, $count = 1) {
    return (($num > 0) ? str_repeat($data, $count) : '');
}


// End Of Helper "String"
}